package com.waitingsnow.common.share.netEaseMicroblog.data
{
	import com.waitingsnow.common.share.bridia.net.oauth.BDOAuthConnection;
	import com.waitingsnow.common.share.bridia.net.oauth.OauthEvent;
	import com.waitingsnow.common.share.netEaseMicroblog.NEMicroblog;
	import com.waitingsnow.common.share.config.AppInfo;
	
	import flash.events.Event;
	import flash.external.ExternalInterface;
	import flash.net.SharedObject;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	/**
	 * 
	 * @author Tensharp
	 * 
	 * <p>负责用户登陆部分的OAuth访问.</p>
	 * 
	 */	
	
	public class NEMOAuth extends NEMObject
	{
		/**
		 * 
		 * <p>创建一个新的NEMOauth对象.</p>
		 * 
		 * @param parent 上级对象.(NEMicroblog)
		 * @param oauth OAuth链接
		 * 
		 */		
		
		public function NEMOAuth(parent:NEMicroblog, oauth:BDOAuthConnection)
		{
			super (parent, oauth);
		}
		
		/**
		 * 
		 * <p>有xAuth权限时可以从这里登陆.</p>
		 * 
		 * @param username 用户名
		 * @param password 密码
		 * 
		 */	
		
		public function login (username:String, password:String):void
		{
			
		}
		
		/**
		 * 
		 * <p>获取未授权的Request Token(oauth/request_token)</p>
		 * 
		 */	
		
		public function requestToken ():void
		{
			super.connect ("REQUEST_TOKEN", "GET", false);
		}
		
		/**
		 * 
		 * <p>弹出验证码.</p>
		 * 
		 * <ul>
		 * 	<li>NEMicroblog会判断应用是否为客户端应用.如果是客户端应用会强制转到弹出验证码页面.</li>
		 * 	<li>web应用会直接弹出请求用户授权页.</li>
		 * </ul>
		 * 
		 */
		
		public function authorize (callback:String = null):void
		{
//			navigateToURL (oauth.getOAuthRequest ("GET", uri ("AUTHORIZE", false)));
            oauth.addOAuthParameters ("oauth_callback", callback, true);
            var req:URLRequest = oauth.getOAuthRequest ("GET", uri ("AUTHENTICATE", false));
            var uri:String = req.url + "?" +  "oauth_token=" + oauth.oauth_token + "&oauth_callback=" + AppInfo.CALL_BACK;
            dispatchEvent(new OauthEvent(OauthEvent.NE_OAUTH, uri));
		}
		
		/**
		 * 
		 * <p>请求用户授权.</p>
		 * 
		 * <ul>
		 * 	<li>NEMicroblog会判断应用是否为客户端应用.如果是客户端应用会强制转到弹出验证码页面.</li>
		 * 	<li>web应用会直接弹出请求用户授权页.</li>
		 * </ul>
		 * 
		 * @param callback 请求用户授权后跳转的地址.桌面应用不需要callback.
		 * 
		 */	
		
		public function authenicate (callback:String = null):void
		{
			if (callback && callback.length > 0)
			{
				super.oauth.addOAuthParameters ("oauth_callback", callback, true);
			}
			
			//

			if (ExternalInterface.available && !parent.localMode)
			{
				ExternalInterface.call ("window.open", uri (action, false), 'Authenticate','height=400,width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no');
			}
			else
			{
				navigateToURL (oauth.getOAuthRequest ("GET", uri ("AUTHENTICATE", false, null)));
			}
		}
		
		/**
		 * 
		 * <p>通过Request Token换取Access Token(oauth/access_token)</p>
		 * <p>web应用不需要输入verifier</p>
		 * 
		 * @param verifier 服务器端提供的验证码,桌面客户端使用.
		 * 
		 */
		
		public function accessToken (verifier:String = null):void
		{
			if (verifier && verifier.length > 0)
			{
//				oauth.addOAuthParameters ("oauth_token", verifier, false);
			}
			
			//
			
			super.connect ("ACCESS_TOKEN", "GET", false);
		}
		
		/**
		 * 
		 * @private
		 *  
		 * @param event
		 * 
		 */		
		
		override protected function connectCompleteHandler(event:Event):void
		{
			if (super.action == "ACCESS_TOKEN" || super.action == "REQUEST_TOKEN")
			{
				var ary:Array = event.target.data.toString ().split ("&");

				//
				
				oauth.oauth_token        = ary[0].split ("=")[1];
				oauth.oauth_token_secret = ary[1].split ("=")[1];
                
                var so:SharedObject = SharedObject.getLocal("shareKit/Netease");
                so.data["access_token"] = oauth.oauth_token;
                so.data["access_token_secret"] = oauth.oauth_token_secret;
			}

			//

			super.connectCompleteHandler (event);
		}
	}
}